/* 
 * login module js
 * @author luweijun
 * @todo : sign after post and captcha
 */
(function($,_){
    var Login = function(){
        
    };
    Login.init = function() {
        this.initMouse();
        this.initEvents();
    };
    Login.initMouse = function() {
        $(document).ready(function() {
            $("#login_user").focus();
        });
    };
    Login.initEvents = function() {
        this.listenSubmit();
        this.listenCaptchaRefresh();
        this.listenRemove();
    };
    Login.listenSubmit = function() {
        var _this = this;
        var params = {};
        $("#loginform").submit(function(e){
            if ($("#login_user").val().trim() == ''){
                    _this.showNote('请输入用户名！');
                    $("#login_user").focus();
                    return false;
            }
            params['username'] = $("#login_user").val().trim();
            if ($("#login_pwd").val().trim() == '') {
                _this.showNote('请输入密码！');
                $("#login_pwd").focus();
                return false;
            }
            params['password'] = $("#login_pwd").val().trim();
            if ($("#rand_rs").val().trim() == '') {
                _this.showNote('请输入验证码！');
                $("#rand_rs").focus();
                return false;
            }
            params['captcha'] = $("#rand_rs").val().trim();
            _this.showNote("正在检查用户信息......");
            _this.sign(params);
            e.preventDefault();
            return false;
        });
        Login.showNote = function(note) {
            $(".user-note").html(note);
        };
        Login.sign = function(params) {
            var _this = this;
            params['t'] = new Date().getTime();
            $.ajax({
              type: "POST",
              url: '/admin/sign/in',
              data: params,
              success: function(data){
                  if (data['state'] && data['state']) {
                      _this.showNote('通过验证，正在跳转！');
                      location.href = '/admin/index';
                  } else {
                      _this.showNote('未通过校验，请重试！');
                  }
                  return false;
              },
              error : function(data) {
                  _this.showNote('登录发生错误，请重试！');
                  return false;
              },
              dataType: 'json'
            });
        };
        Login.ReloadCaptcha = function() {
            var url = '/captcha?t='+new Date().getTime();
            $("#login_captcha").attr('src',url);
        };
        Login.listenCaptchaRefresh = function() {
            var _this = this;
            $("#login_captcha").click(function(){
                _this.ReloadCaptcha();
            });
        };
        Login.listenRemove = function() {
            $("#login_user").keyup(function(){
                if ($(this).val().trim() != '') {
                    $(".user-note").html('');
                }
            });
             $("#login_pwd").keyup(function(){
                if ($(this).val().trim() != '') {
                    $(".user-note").html('');
                }
            });
             $("#rand_rs").keyup(function(){
                if ($(this).val().trim() != '') {
                    $(".user-note").html('');
                }
            });
        };
    };
    umams.admin.Login = Login;
})(jQuery,_);


